package org.csu.mypetstore.persistence.impl;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.CategoryDAO;
import org.csu.mypetstore.persistence.impl.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;//自动导入？已设置不知道能不能用

public class CategoryDAOImpl implements CategoryDAO
{
    //private static final String GET_CATEGORY_LIST="SELECT CATID AS categoryId,NAME,DESCN AS description FROM CATEGORY";
    //private static final String GET_CATEGORY="select * from category where catid=?";
    private static final String GET_CATEGORY_LIST = "SELECT CATID AS categoryId,NAME,DESCN AS description FROM CATEGORY";

    private static final String GET_CATEGORY = "SELECT CATID AS categoryId,NAME,DESCN AS description FROM CATEGORY WHERE CATID = ?";
    @Override
    public List<Category> getCategoryList() {
        List<Category> categoryList=new ArrayList<Category>();
        try{
            Connection connection= DBUtil.getConnection();
            PreparedStatement pStatement=connection.prepareStatement(GET_CATEGORY_LIST);
            ResultSet resultSet=pStatement.executeQuery();
            while(resultSet.next())
            {
                Category category=new Category();
                category.setCategoryId(resultSet.getString(1));
                category.setName(resultSet.getString(2));
                category.setDescription(resultSet.getString(3));
                categoryList.add(category);

            }
            DBUtil.close(connection,pStatement,resultSet);
        }catch (Exception e){
            e.printStackTrace();
        }
        return categoryList;
    }

    @Override
    public Category getCategory(String categoryId) {
        Category category=null;
        try{
            Connection connection =DBUtil.getConnection();
            PreparedStatement pStatement=connection.prepareStatement(GET_CATEGORY);
            pStatement.setString(1,categoryId);
            ResultSet resultSet=pStatement.executeQuery();
            if (resultSet.next())
            {
                category=new Category();
                category.setCategoryId(resultSet.getString(1));
                category.setName(resultSet.getString(2));
                category.setDescription(resultSet.getString(3));
//                System.out.println("hhhh");
            }
            DBUtil.close(connection,pStatement,resultSet);
        }catch (Exception e){
            e.printStackTrace();
        }
        return category;
    }

//    @Override
//    public List<Product> getProductListByCategory(String categoryId) {
//        return null;
//    }
}
